package edu.uwlax.cs.oayonlinestore.client.gui.shared;

import java.util.Date;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.User;
import edu.uwlax.cs.oayonlinestore.client.utils.FormTable;
import edu.uwlax.cs.oayonlinestore.client.utils.DateEdit.InvalidDateException;
import edu.uwlax.cs.oayonlinestore.vo.UserDetailsVO;

public class AccountForm extends FormTable {

	private AddressEdit address = new AddressEdit();

	private CreditCardEdit creditCard = new CreditCardEdit();

	private TextBox lgEmail = new TextBox();

	private TextBox lgLogin = new TextBox();

	private PasswordTextBox lgPassword1 = new PasswordTextBox();

	private PasswordTextBox lgPassword2 = new PasswordTextBox();

	private TextBox piFirstname = new TextBox();

	private TextBox piLastName = new TextBox();

	private TextBox piPhone = new TextBox();

	private CheckBox rolCanBeCategoryPerson = new CheckBox();

	private CheckBox rolIsAdmin = new CheckBox();

	private CheckBox rolIsIndustrialCustomer = new CheckBox();

	private CheckBox rolIsNormalCustomer = new CheckBox();

	private CheckBox rolIsSalesManager = new CheckBox();

	private CheckBox rolIsSalesStaff = new CheckBox();

	private UserDetailsVO user;

	public AccountForm(boolean createUser, boolean editRoles) {
		super();

		if (editRoles) {
			addSection("Roles");
			addInput("Is Admin", rolIsAdmin);
			addInput("Is Sales Manager", rolIsSalesManager);
			addInput("Is Sales Staff", rolIsSalesStaff);
			addInput("Can be Category Person", rolCanBeCategoryPerson);
			addInput("Is Normal Customer", rolIsNormalCustomer);
			// addInput("Is Industrial Customer", rolIsIndustrialCustomer);
		}
		addInput("I am an Industrial User", rolIsIndustrialCustomer);
		addSection("Login Information");
		addInput("Login Name", lgLogin);
		addInput("Password", lgPassword1);
		addInput("Password (confirm)", lgPassword2);
		addInput("Email Address", lgEmail);
		addSection("Personal Information");
		addInput("First Name", piFirstname);
		addInput("Last Name", piLastName);
		addInput("Phone", piPhone);
		addInput(address);

		if (!createUser || editRoles) {
			addSection("Payment information");
			addInput(creditCard);
		}
		if (!createUser)
			addButton("Save", null);
		else {
			addButton("Create Account", null);
			onDataLoad(getNewUser());
		}
	}

	private UserDetailsVO getNewUser() {
		return new UserDetailsVO(0, "", "", true, "", "", "", "", "", "", 0,
				"", "", "", new Date(), false, false, false, false, true, false);
	}

	public void loadUser(int userID) {
		if (userID != 0)
			OnlinestoreRService.Util.getInstance().getUserDetail(userID,
					getLoadHandler());
	}

	public void onButtonClicked() throws InvalidDateException {
		user.setCity(address.getCity().getText());
		user.setState(address.getState().getText());
		user.setStreetAddr(address.getStreet().getText());
		user.setZipcode(Integer.parseInt(address.getZip().getText()));
		String ccNum = creditCard.getNo().getText();
		if (ccNum.length() != 16)
			addError("Credit card number should be 16 digits !");
		user.setCcNumber(ccNum);

		user.setCcExpirationDate(creditCard.getExpDate().getDate());
		user.setCcCompany(creditCard.getInstitute().getSelection());

		user.setEmail(lgEmail.getText());
		user.setLogin(lgLogin.getText());
		user.setPassword(lgPassword1.getText());
		if (!lgPassword1.getText().equals(lgPassword2.getText()))
			addError("Both passwords must be the same");

		user.setFirstname(piFirstname.getText());
		user.setLastname(piLastName.getText());
		user.setTeleNumber(piPhone.getText());

		user.setCanBeCategoryPerson(rolCanBeCategoryPerson.isChecked());
		user.setIsAdministrator(rolIsAdmin.isChecked());
		user.setIsIndustrialCustomer(rolIsIndustrialCustomer.isChecked());
		user.setIsNormalCustomer(rolIsNormalCustomer.isChecked());
		user.setIsSalesManager(rolIsSalesManager.isChecked());
		user.setIsSalesStaff(rolIsSalesStaff.isChecked());
		//

		user.setIsActive(true);

		user.setCcCompany(creditCard.getInstitute().getItemText(
				creditCard.getInstitute().getSelectedIndex()));
		boolean hasRole = rolCanBeCategoryPerson.isChecked()
				|| rolIsAdmin.isChecked()
				|| rolIsIndustrialCustomer.isChecked()
				|| rolIsNormalCustomer.isChecked()
				|| rolIsSalesManager.isChecked() || rolIsSalesStaff.isChecked();
		if (!hasErrors() && hasRole)
			OnlinestoreRService.Util.getInstance().addEditUser(user,
					getSaveHandler());

		if (!hasRole)
			Window.alert("User Must have a Role");
	}

	public void onDataLoad(Object data) {
		user = (UserDetailsVO) data;

		address.getCity().setText(user.getCity());
		address.getState().setText(user.getState());
		address.getStreet().setText(user.getStreetAddr());
		address.getZip().setText(String.valueOf(user.getZipcode()));

		creditCard.getNo().setText(user.getCcNumber());
		creditCard.getExpDate().setDate(user.getCcExpirationDate());
		creditCard.getInstitute().setSelection(user.getCcCompany());

		lgEmail.setText(user.getEmail());
		lgLogin.setText(user.getLogin());
		lgPassword1.setText("");
		lgPassword2.setText("");

		piFirstname.setText(user.getFirstname());
		piLastName.setText(user.getLastname());
		piPhone.setText(user.getTeleNumber());

		rolCanBeCategoryPerson.setChecked(user.isCanBeCategoryPerson());
		rolIsAdmin.setChecked(user.isIsAdministrator());
		rolIsIndustrialCustomer.setChecked(user.isIsIndustrialCustomer());
		rolIsNormalCustomer.setChecked(user.isIsNormalCustomer());
		rolIsSalesManager.setChecked(user.isIsSalesManager());
		rolIsSalesStaff.setChecked(user.isIsSalesStaff());
	}

	public void onDataSaved() {
		if (user.getId() == User.get().getId())
			User.get().update();
	}
}
